﻿using System.Linq;
using System.Reflection;
using DC2010.Objects;
using DC2010.Objects.Base;
using DC2010.Objects.Data;
using log4net;

namespace DC2010.Effects
{
    public class I_JoinParty_Effect : EffectLogic
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public I_JoinParty_Effect(EffectData ed)
            : base(ed)
        {

        }

        public override void Instant(BaseObject aInvoker, BaseObject aTarget)
        {
            if ((aTarget is PartyData) && (aInvoker is CreatureData))
            {
                CreatureData c = (CreatureData)aInvoker;
                PartyData p = (PartyData)aTarget;

                //can i join party?
                bool cond = Root.I.ConditionSystem.Instances.C_CanJoinParty.CheckCondition(c, p);

                if (cond)
                {
                    p.L.AddToFormation(c.Id);
                    p.MembersData.Add(c);
                    p.Members.Add(c.Id);

                    // alive....
                    c.L.Start();


                    //is there a party leader?
                    if (string.IsNullOrEmpty(p.PartyLeaderId))
                    {
                        p.PartyLeaderId = c.Id;
                    }
                }

                log.InfoFormat("{0} join party - {1}", c.Id, p.Id);
            }

            base.Instant(aInvoker, aTarget);
        }
    }
}
